<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增短链接')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-url-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">目标地址：</label>
                <div class="col-sm-8">
                    <input id="targetUrl" name="targetUrl" class="form-control" type="text" required>
                </div>
            </div>
            
            <div class="form-group">
		        <label class="col-sm-3 control-label">多少天后失效(-1表示永久)：</label>
		        <div class="col-sm-8">
		            <div class="input-group date">
                        <input type="number" min="-1" class="form-control" name="expirationTime" id="expirationTime" placeholder="天" th:value="${expireDay==null?-1:expireDay}" th:disabled="${expireDay!=null}">
                    </div>
		        </div>
		    </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script type="text/javascript">
    jQuery.validator.addMethod("isRightUrl", function(value, element, param) {
        return this.optional(element) || NetPing(value);
    }, "请填写正确的目标地址url");
    
    jQuery.validator.addMethod("expirationTime", function(value, element, param) {
        return this.optional(element) || (value>0||value==-1);
    }, "必须大于一天");
    
        var prefix = ctx + "short/url"
        $("#form-url-add").validate({
            focusCleanup: true,
            rules: {
            	targetUrl: {
                  required: true,
                  isRightUrl: true
                },
                expirationTime: {
                	required: true,
                	expirationTime: true
                }
              }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-url-add').serialize());
            }
        }
        
        function NetPing(urlForValid) {
        	var r = true;
			if(urlForValid.indexOf('http')<0){
				r = false;
			}else{
				$.ajax({
					url: urlForValid,
					async: false,
					type: "GET",
					dataType: "jsonp",
					timeout:10000,
					complete: function(response, textStatus) {
						if(response.status == 200){
							r = true;
						}else{
							r = false;
						}
					}
				});
			}
			return r;
		}
    </script>
</body>
</html>